<?php

namespace paper\http\response;

class JsonResponse extends Response
{
    // 输出参数
    protected $options = [
        'json_encode_param' => JSON_UNESCAPED_UNICODE,
    ];

    protected string $contentType = 'application/json';

    /**
     * 处理数据
     * @access protected
     * @param mixed $data 要处理的数据
     * @return mixed
     */
    protected function output($data)
    {
        // 返回JSON数据格式到客户端 包含状态信息
        $data = json_encode($data, $this->options['json_encode_param']);
        if (false === $data) {
            throw new \InvalidArgumentException(json_last_error_msg());
        }
        return $data;
    }

    protected function contentType(): string
    {
        return Response::CONTENT_TYPE_JSON;
    }
}